package com.ljc.cjgl.service;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.ljc.cjgl.custom.FileUtil;
import com.ljc.cjgl.entitiy.Stu;
import com.ljc.cjgl.form.StudentForm;
import com.ljc.cjgl.repository.StuRepository;
import com.ljc.cjgl.security.SysRole;

@Service
public class StuService {
	@Autowired
	private StuRepository stuDAO;
	
	public Stu findById(Integer id) {
		Optional<Stu> optional = stuDAO.findById(id);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}
	
	public List<Stu> findAll(){
		return stuDAO.findAll();
	}
	
	public Page<Stu> findAll(Specification<Stu> specification,Pageable pageable){
		return stuDAO.findAll(specification, pageable);
	}
	
	@Transactional
	public synchronized boolean save(MultipartFile file, StudentForm form) throws IOException {
		BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
		Stu student=new Stu();
		Integer id = form.getId();
		if(id!=null) {
			student=findById(id);
		}else {
			student.setPassword(encoder.encode("111111"));
		}
		String stuNum = form.getStuNum();
		if(StringUtils.hasText(stuNum)) {
			student.setUsername(stuNum);
		}
		try {
			BeanUtils.copyProperties(form, student,"id","photoName","saveName");
			String fileName = file.getOriginalFilename();
			if(StringUtils.hasText(fileName)) {
				student.setPhotoName((new File(fileName)).getName());
				String uid=UUID.randomUUID().toString();
				student.setSaveName(uid);
				FileUtil.uploadFile(file.getBytes(), uid);
			}
			List<SysRole> roles=new ArrayList<SysRole>();
			roles.add(new SysRole(1));
			student.setRoles(roles);
			stuDAO.save(student);
			return true;
		}catch (Exception e) {
			return false;
		}
	}
	
	public Stu findByUid(String uid) {
		Optional<Stu> optional = stuDAO.findBySaveName(uid);
		if(optional.isPresent()) {
			return optional.get();
		}
		return null;
	}

	public synchronized boolean delete(List<Stu> students) {
		try {
			for (Stu stu : students) {
				stuDAO.deleteById(stu.getId());
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public List<Stu> findAll(Sort sort) {
		return stuDAO.findAll(sort);
	}

	public List<Stu> findByTbClassId(Integer classId) {
		return stuDAO.findByTbClassId(classId);
	}

	public void save(Stu stu) {
		stuDAO.save(stu);
		
	}

}
